/*************************************************
Name : CaseEscalatorScheduler
Created By : dthekumparampil
Created Date : Feb 7, 2013
Description : 	An Apex class created to schedule itself to run after an hour if 
				there is no open cases matching a criterion. If it finds any Cases
				then it would trigger the batch job in CaseEscalator Class.
********************************
Modified By : 
Modified Date : 
Modification : 	
*************************************************/

global class CaseEscalatorScheduler implements Schedulable{
	/****
	In the below method, we search for all cases for which the West_Business_Age_Flag__c is not set.
	If it finds any, it would call the CaseEscalator Apex Batch to calculate the Age of Case
	and escalate the priority if required.
	If it doesn't finds any, it would schedule itself to run again after a time period defined
	by the custom settings value in West_Scheduled_Job_Timer__c. 
	****/
	global void execute(SchedulableContext SC){
		
		String caseRecordType = Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - Support').getRecordTypeId();
		String caseADARecordType = Schema.Sobjecttype.Case.getRecordTypeInfosByName().get('West - ADA Support').getRecordTypeId();
		
		list<Case> listOfMatchingCases = [select id, status, Priority,
										West_Business_Age_Flag__c,West_Business_Age__c,
										CreatedDate, Case_Name__c
										from Case
										where 	//status='Closed' AND 
										West_Business_Age_Flag__c= false 
										AND (RecordTypeId =: caseRecordType OR RecordTypeId=:caseADARecordType)];
		
		system.debug('>>>>>>>>>>>>>>number of cases: '+listOfMatchingCases.size());
		if(listOfMatchingCases.size()>0){
			Database.executeBatch(new CaseEscalator(),2000);
		}
	}
}